package com.ruoyi.project.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.project.system.entity.SysRole;
import com.ruoyi.project.system.entity.SysRoleMenu;
import com.ruoyi.project.system.entity.SysUserRole;
import com.ruoyi.project.system.mapper.SysRoleMapper;
import com.ruoyi.project.system.mapper.SysUserRoleMapper;
import com.ruoyi.project.system.service.ISysRoleMenuService;
import com.ruoyi.project.system.service.ISysRoleService;
import com.ruoyi.project.system.vo.SysRoleMenuVO;
import lombok.AllArgsConstructor;
import net.dreamlu.mica.core.utils.$;
import net.dreamlu.mica.mybatis.service.impl.MicaServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 角色 业务层实现类
 *
 * @author dy
 */
@Service
@AllArgsConstructor
public class SysRoleServiceImpl extends MicaServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {

    private final ISysRoleMenuService sysRoleMenuService;

    private final SysRoleMapper roleMapper;

    private final SysUserRoleMapper userRoleMapper;

    @Override
    public List<SysRole> listUserRoleByUserId(Long userId) {
        // sys_user_role
        List<SysUserRole> userRoleList = userRoleMapper.selectList(
                new LambdaQueryWrapper<SysUserRole>()
                        .eq(SysUserRole::getUserId, userId));

        if (!CollectionUtils.isEmpty(userRoleList)) {
            // sys_role
            return roleMapper.selectList(new LambdaQueryWrapper<SysRole>()
                    .in(SysRole::getId, userRoleList.stream().map(SysUserRole::getRoleId).collect(Collectors.toList())));
        }
        return new ArrayList<>();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addRoleMenu(SysRoleMenuVO sysRoleMenuVO) {
        SysRole sysRole = $.copy(sysRoleMenuVO, SysRole.class);
        sysRole.setDataScope(3);
        int result = roleMapper.insert(sysRole);
        return result > 0 && submitRoleMenus(sysRole.getId(), sysRoleMenuVO.getMenuIds());

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateRoleMenu(SysRoleMenuVO sysRoleMenuVO) {
        SysRole sysRole = $.copy(sysRoleMenuVO, SysRole.class);
        int result = roleMapper.updateById(sysRole);
        return result > 0 && submitRoleMenus(sysRole.getId(), sysRoleMenuVO.getMenuIds());
    }

    /**
     * 新增修改角色菜单
     */
    public boolean submitRoleMenus(Long roleId, List<Long> menuIds) {
        sysRoleMenuService.remove(new LambdaQueryWrapper<SysRoleMenu>().eq(SysRoleMenu::getRoleId, roleId));
        Set<SysRoleMenu> sysRoleMenus = new HashSet<>();
        if ($.isNotEmpty(menuIds)) {
            menuIds.forEach(v -> {
                SysRoleMenu sysRoleMenu = new SysRoleMenu();
                sysRoleMenu.setRoleId(roleId);
                sysRoleMenu.setMenuId(v);
                sysRoleMenus.add(sysRoleMenu);
            });
            return sysRoleMenuService.saveIgnoreBatch(sysRoleMenus);
        }
        return true;
    }
}
